package com.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.annotation.DataSource;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.entity.SysUser;
import com.enums.DataSourceEnum;
import com.mapper.SysUserMapper;
import com.service.SysUserService;
import com.utils.PasswordUtils;
import com.utils.Public;
import com.utils.R;
import com.utils.RsaUtils;
import com.utils.SessionUtils;

/**
* 
*
* @author Zyuan
* @version 创建时间：2019年6月22日 下午8:08:10
*/

@Service
public class SysUserServiceimpl extends ServiceImpl<SysUserMapper,SysUser> implements SysUserService  {

	@Autowired
	private SysUserMapper userMapper;
	
	@Value("${spring.role.wxry}")
	private String wxryId;
	
	@Value("${spring.role.qxrzr}")
	private String qxrzr;
	
	@Override
	@DataSource(DataSourceEnum.DB2)
	public R login(Map<String, Object> params) {
	
		String pwd = RsaUtils.decryptByPrivateKey(Public.mapTo(params.get("password"), ""), Public.mapTo(params.get("privateKey"), ""));
        if(Public.mapTo(pwd, "").length() <= 0){
        	return R.error("密码错误");
        }
		Map<String, Object> requestMap = new HashMap<>();
		requestMap.put("loginId", Public.mapTo(params.get("loginId"), ""));
		Map<String, Object> data = userMapper.getUserInfo(requestMap);
		if(data == null){
			return R.error("账号错误,请检查");
		}
		
		if(!PasswordUtils.validatePassword(pwd, Public.mapTo(data.get("PASSWORD"), ""))){
			return R.error("密码错误");
		}
		
		return R.success(data);
	}

	  /**
     * 获取request
     * @return
     */
    public static HttpServletRequest getRequest(){  
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();  
        return requestAttributes==null? null : requestAttributes.getRequest();  
    }  
      /**
       * 获取session
       * @return
       */
    public static HttpSession getSession(){  
        return getRequest().getSession(false);  
    }  
	
	@Override
	@DataSource(DataSourceEnum.DB2)
	public R updatePassword(Map<String, Object> params) {

		Map<String, Object> searchParam = new HashMap<String, Object>();
		searchParam.put("phone", Public.mapTo(params.get("phone"), ""));
		Map<String, Object> data =  userMapper.getUserInfo(searchParam);
        if(data == null){
        	return R.error("账号不存在");
        }
		String pwd = RsaUtils.decryptByPrivateKey(Public.mapTo(params.get("password"), ""), Public.mapTo(params.get("privateKey"), ""));
        SysUser userView = new SysUser();
        userView.setPassword(PasswordUtils.entryptPassword(pwd));
		this.update(userView, new EntityWrapper<SysUser>().eq("ID", Public.mapTo(data.get("ID"), "")));
        return R.ok();
	}

	@Override
	@DataSource(DataSourceEnum.DB2)
	public Map<String, Object> getUserInfo(Map<String, Object> params) {
		Map<String, Object> data = userMapper.getUserInfo(params);
		return data;
	}

	@Override
	@DataSource(DataSourceEnum.DB2)
	public Page<Map<String, Object>> getMaintainList(Map<String, Object> params) {
        Page<Map<String, Object>> pageBean = new Page<>(Public.mapTo(params.get("page"), 1), Public.mapTo(params.get("pageSize"), 10));
		params.put("roleId", wxryId);
		List<Map<String, Object>> list = userMapper.getMaintainList(params);
		Page<Map<String, Object>> pageInfo = pageBean.setRecords(list);
		return pageInfo;
	}

	@Override
	@DataSource(DataSourceEnum.DB2)
	public List<Map<String, Object>> getAreaManagerViewList(Map<String, Object> params) {	
		params.put("roleId", qxrzr);
		return userMapper.getAreaManagerViewList(params);
	}

}
